package org.grayrabbit.cms.component;

import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractComponent {
	protected Logger LOG = LoggerFactory.getLogger(AbstractComponent.class);
	private static final String REG="@";
	private static final String MIDDLE_LINE="-";
	
	
	public  String loadComponentInfo(String component,String parameters){
		String[] ps = splitParameters(parameters); 
		Map<String, String> map= loadparameterMap(component, ps);
		Set<String> set = map.keySet(); 
		for(String k : set){ 
			component = component.replace("${"+k.toUpperCase()+"}", " "+map.get(k)+" ");
		}
		return component;
	}
	protected abstract Map<String, String> loadparameterMap(String component,String[] ps);
  	
	String[] splitParameters(String parameters){
		String[] ps = parameters.split(REG);
		for(int i=0;i<ps.length;i=i+2){
			if(MIDDLE_LINE.equals(ps[i+1]))
				ps[i+1]=null;
		}
		return  ps;
	}
	
	String ArrayToString(Object[] objs,boolean flag){		 
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		if(!ArrayUtils.isEmpty(objs)){
			for(int i=0;i<objs.length;i++){
				if(flag){ 
					sb.append("'"+objs[i]+"'");
				}else{
					sb.append(objs[i]);
				}
				if(i<objs.length-1) sb.append(","); 
			}
		}
		sb.append("]"); 
		return sb.toString();		
	}
	/**
	 * 字符转中的中文逗号转英文逗号
	 * @return
	 */
	public static String transferComma(String exp){
		if(StringUtils.isBlank(exp))
			return exp; 
		return exp.replace("，", ",");
	}
 
}
